//
//  TTSubOrAddView.m
//  TaoTuo
//
//  Created by admin on 2019/8/26.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTSubOrAddView.h"

@interface TTSubOrAddView ()

@property (nonatomic, strong) UIButton *subBtn;
@property (nonatomic, strong) UILabel *textLabel;
@property (nonatomic, strong) UIButton *addBtn;
///为了加大按钮点击区域故意这样
@property (nonatomic, strong) UILabel *addLabel;

@property (nonatomic, copy) buttonClickBlock block;


@end

@implementation TTSubOrAddView

+ (instancetype)subAddViewWithMin:(int)min max:(int)max clickBtnBlock:(nonnull buttonClickBlock)clickBtnBlock
{
    TTSubOrAddView *view = [[TTSubOrAddView alloc] initWithMin:min max:max clickBtnBlock:clickBtnBlock];
    return view;
}

- (instancetype)initWithMin:(int)min max:(int)max clickBtnBlock:(nonnull buttonClickBlock)clickBtnBlock
{
    self = [super init];
    if (self) {
        
        
        self.minValue = min;
        self.maxValue = max;
        self.block = clickBtnBlock;
        
        self.currentValue = min;
        
        [self setupSubviews];
        
    }
    return self;
}

- (void)setCurrentValue:(int)currentValue
{
    _currentValue = currentValue;
    
    NSString *str = [NSString stringWithFormat:@"%d", _currentValue];
    self.textLabel.text = str;
    
}

// 减少
- (void)subButtonClick
{
    if (self.currentValue <= self.minValue) {
        self.currentValue = self.minValue;
        return;
    }
    
    self.currentValue--;
    
    NSString *str = [NSString stringWithFormat:@"%d", self.currentValue];
    self.textLabel.text = str;
    if (self.block) {
        self.block(self.currentValue);
    }
    
    UIColor *color = self.currentValue <= self.minValue ? UIColorHex(EFEFEF) : [UIColor whiteColor];
    self.subLabel.layer.backgroundColor = color.CGColor;
}

// 添加
- (void)addButtonClick
{
    if (self.currentValue >= self.maxValue) {
        self.currentValue = self.maxValue;
        return;
    }
    
    self.currentValue++;
    
    NSString *str = [NSString stringWithFormat:@"%d", self.currentValue];
    self.textLabel.text = str;
    
    if (self.block) {
        self.block(self.currentValue);
    }
    
    self.subLabel.layer.backgroundColor = [UIColor whiteColor].CGColor;
    
}


- (void)setupSubviews
{
    _subBtn = [UIButton createButtonWithImage:nil target:self action:@selector(subButtonClick)];
//    _subBtn = [UIButton createButtonWithTitle:@"-" titleColor:UIColorHex(999999) titleFont:[UIFont systemFontOfSize:15] backgroundColor:[UIColor cyanColor] cornerRadius:2 target:self action:@selector(subButtonClick)];
//    _subBtn.layer.borderColor = UIColorHex(C3C3C3).CGColor;
//    _subBtn.layer.borderWidth = 0.5;
    [self addSubview:_subBtn];
    [_subBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.centerY.equalTo(self);
        make.width.mas_equalTo(40);
    }];
    
    _subLabel = [UILabel labelTextCenterWithTitle:@"-" color:UIColorHex(999999) fontSize:15];
    _subLabel.layer.backgroundColor = UIColorHex(EFEFEF).CGColor;
    _subLabel.layer.borderColor = UIColorHex(C3C3C3).CGColor;
    _subLabel.layer.borderWidth = 0.5;
    [_subBtn addSubview:_subLabel];
    [_subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.centerY.equalTo(self.subBtn);
        make.width.height.mas_equalTo(20);
    }];
    
    NSString *str = [NSString stringWithFormat:@"%d", self.minValue];
    _textLabel = [UILabel labelTextCenterWithTitle:str color:UIColorHex(333333) fontSize:12];
    _textLabel.layer.cornerRadius = 3;
    _textLabel.layer.borderColor = UIColorHex(C3C3C3).CGColor;
    _textLabel.layer.borderWidth = 0.5;
    _textLabel.backgroundColor = [UIColor whiteColor];
    [self addSubview:_textLabel];
    [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.subBtn.mas_right).mas_offset(5);
        make.centerY.equalTo(self);
        make.width.mas_equalTo(34);
        make.height.mas_equalTo(20);
    }];
    
    _addBtn = [UIButton createButtonWithImage:nil target:self action:@selector(addButtonClick)];
//    _addBtn = [UIButton createButtonWithTitle:@"+" titleColor:UIColorHex(333333) titleFont:[UIFont systemFontOfSize:15] backgroundColor:[UIColor cyanColor] cornerRadius:2 target:self action:@selector(addButtonClick)];
//    _addBtn.layer.borderColor = UIColorHex(C3C3C3).CGColor;
//    _addBtn.layer.borderWidth = 0.5;
    [self addSubview:_addBtn];
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.textLabel.mas_right).mas_offset(5);
        make.centerY.height.equalTo(self);
        make.width.mas_equalTo(40);
    }];
    
    _addLabel = [UILabel labelTextCenterWithTitle:@"+" color:UIColorHex(333333) fontSize:15];
    _addLabel.layer.backgroundColor = UIColorHex(FFFFFF).CGColor;
    _addLabel.layer.borderColor = UIColorHex(C3C3C3).CGColor;
    _addLabel.layer.borderWidth = 0.5;
    _addLabel.layer.masksToBounds = YES;
    [_addBtn addSubview:_addLabel];
    [_addLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.centerY.equalTo(self.addBtn);
        make.width.height.mas_equalTo(20);
    }];
    
    
}


@end
